package top.cluski.tokendemo.expression;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import top.cluski.tokendemo.domain.LoginUser;

import java.util.HashSet;
import java.util.Set;

@Component
public class MyExpressionRoot {

    public boolean hasAuthority(String authority) {
        // 获取当前用户的权限
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Set<String> permissions = new HashSet<>(loginUser.getPermissions());
        // 校验权限
        return permissions.contains(authority);
    }
}
